package com.java.zhuiyun.api.teacher.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.java.zhuiyun.api.sys_dict.entity.SysDict;
import com.java.zhuiyun.api.sys_user.entity.SysUser;
import com.java.zhuiyun.api.sys_user.mapper.SysUserMapper;
import com.java.zhuiyun.api.teacher.entity.Teacher;
import com.java.zhuiyun.api.teacher.mapper.TeacherMapper;
import com.java.zhuiyun.api.teacher.model.TeacherModel;
import com.java.zhuiyun.api.teacher.service.TeacherService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.Md5Utils;
import com.java.zhuiyun.util.RedisUtil;
import com.java.zhuiyun.util.RegexUtils;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName BackCourseServiceImpl
 * @Description TODO
 * @Author 杨友朋
 * @Date 2023/8/14 15:15
 **/
@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    TeacherMapper teacherMapper;

    @Resource
    SysUserMapper sysUserMapper;

    /**
     * @Author 杨友朋
     * @Description TODO 添加教师信息
     * @Date 2023/8/15 11:14
     * @Return
     * @param
     */
    @Override
    @Transactional
    public CommonResult addTeacher(MultipartFile teacherImg, MultipartFile educationImg, TeacherModel teacher) {
        //验证手机号格式
        boolean phoneInvalid = RegexUtils.isPhoneInvalid(teacher.getPhone());
        if(phoneInvalid){
            return CommonResult.error(500,"手机号格式验证失败,请检查是否有误！！！");
        }
        //验证邮箱格式
        boolean emailInvalid = RegexUtils.isEmailInvalid(teacher.getEmail());
        if(emailInvalid){
            return CommonResult.error(500,"邮箱格式验证失败,请检查是否有误！！！");
        }
        //图片存入服务器
        String image = UploadFileUtils.uploadFile(teacherImg);
        if(teacherImg.equals("")){
            return CommonResult.error(500,"图片信息有误！！！");
        }
        //图片存入服务器
        String s=UploadFileUtils.uploadFile(educationImg);
        if(s.equals("")){
            return CommonResult.error(500,"图片信息有误！！！");
        }
        //获取总条数
        Integer integer = teacherMapper.selectCountTeacher();
        //生成直播号
        String code="T_"+ String.format("%06d",integer+1);
        SysUser sysUser=new SysUser();
        BeanUtil.copyProperties(teacher,sysUser);
        sysUser.setUserName(code);
        sysUser.setNickName(teacher.getTeacherName());
        sysUser.setAvatarUrl(image);
        sysUser.setRole("ROLE_TEACHER");
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        sysUser.setPassword(Md5Utils.getMd5(sysUser.getPassword()));
//        System.out.println(sysUser.getPassword());
        sysUserMapper.insertInfo(sysUser);
        if(sysUser.getId()!=0&&sysUser.getId()!=null){
            Teacher t=new Teacher();
            BeanUtil.copyProperties(teacher,t);
            //存入教师照片id值
            t.setTeacherImg(image);
            //存入学历证明照片id值
            t.setEducationImg(s);
            //存入直播间号
            t.setLiveNumber(code);
            //存入userid
            t.setUserId(sysUser.getId());
            //创建时间
            t.setCreateTime(new Date());
            //修改时间
            t.setUpdateTime(new Date());
            //添加方法
            int i = teacherMapper.insertSelective(t);
            if(i>0){

                return CommonResult.ok();
            }
            return CommonResult.error(500,"添加教师信息失败");
        }
        return CommonResult.error(500,"添加教师信息失败");

    }

    /**
     * @Author 杨友朋
     * @Description TODO 全查教师
     * @Date 2023/8/15 11:14
     * @Return
     * @param
     */
    @Override
    public PageResult selectTeacher(TeacherModel teacherModel, PageRequest pageRequest) {
        // 使用MybatisPageHelper工具类开始分页
        MybatisPageHelper.startPage(pageRequest);

        // 查询符合条件的教师信息列表
        List<TeacherModel> teacherModels = teacherMapper.selectTeacher(teacherModel);

        // 使用MybatisPageHelper工具类获取分页结果对象
        PageResult pageResult = MybatisPageHelper.getPageResult(teacherModels);

        return pageResult;
    }

    @Override
    public List<Teacher> selectTeacher() {
        List<Teacher> teachers = teacherMapper.selectT();
        return teachers;
    }

    /**
     * @Author 杨友朋
     * @Description TODO 查询学历菜单
     * @Date 2023/8/16 8:35
     * @Return
     * @param
     */
    @Override
    public List<SysDict> selectEducationDict(){

        String type="education";
        List<SysDict> dict=teacherMapper.selectEducationDict(type);
        return dict;
    }

    /**
     * @Author 杨友朋
     * @Description TODO 删除课程
     * @Date 2023/8/16 19:30
     * @Return
     * @param
     */
    @Override
    public CommonResult delTeacher(Integer id) {
        if(id!=null&&id!=0){
            Integer integer = teacherMapper.delTeacher(id);
            if(integer>0){
                return CommonResult.ok();
            }
            return CommonResult.error(500,"删除失败");
        }
        return CommonResult.error(500,"id为null");
    }

    /**
     * @Author 杨友朋
     * @Description TODO 修改教师
     * @Date 2023/8/16 20:09
     * @Return
     * @param
     */
    @Override
    @Transactional
    public CommonResult upTeacher(MultipartFile teacherImg, MultipartFile educationImg, TeacherModel teacher) {
        //验证手机号格式
        boolean phoneInvalid = RegexUtils.isPhoneInvalid(teacher.getPhone());
        if(phoneInvalid){
            return CommonResult.error(500,"手机号格式验证失败,请检查是否有误！！！");
        }
        //验证邮箱格式
        boolean emailInvalid = RegexUtils.isEmailInvalid(teacher.getEmail());
        if(emailInvalid){
            return CommonResult.error(500,"邮箱格式验证失败,请检查是否有误！！！");
        }

        if(teacherImg!=null){
            //图片存入服务器
            String image = UploadFileUtils.uploadFile(teacherImg);
            if(teacherImg.equals("")){
                return CommonResult.error(500,"图片信息有误！！！");
            }
            teacher.setTeacherImg(image);
        }
        if(educationImg!=null){
            //图片存入服务器
            String s=UploadFileUtils.uploadFile(educationImg);
            if(s.equals("")){
                return CommonResult.error(500,"图片信息有误！！！");
            }
            teacher.setEducationImg(s);
        }
            Teacher t=new Teacher();
            BeanUtil.copyProperties(teacher,t);
            //修改时间
            t.setUpdateTime(new Date());
            //添加方法

        Integer i = teacherMapper.upTeacher(t);
        if(i==0){

                return CommonResult.error("修改信息失败");
            }
        SysUser sysUser=new SysUser();
        BeanUtil.copyProperties(teacher,sysUser);
        sysUser.setId(teacher.getUserId());
        sysUser.setUpdateTime(new Date());
        Integer integer = sysUserMapper.updateInfo(sysUser);
        if(integer==0){
            return CommonResult.error("修改信息失败");
        }
        return CommonResult.ok("修改成功");

    }

    @Override
    public CommonResult selectTeacherById(Integer id) {
        return CommonResult.ok(teacherMapper.selectTeacherById(id));
    }
}
